﻿//-----------------------------------------------------------------------
// <copyright file="ContentAggregationToolPart.cs" company="Microsoft 
// Corporation">
// Copyright (c) Microsoft Corporation.
// All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Anil Dwarakanath</author>
// <email>anildwa@microsoft.com</email>
// <date>31-Dec-2010</date>
// <summary>Main Class for Rendering Tool Controls</summary>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebPartPages;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Web.UI;
using System.Reflection;


namespace ContentAggregationWebPart
{
    class ContentAggregationToolPart : Microsoft.SharePoint.WebPartPages.ToolPart
    {

        #region Private Fields
        Panel toolPartPanel;
        #endregion

        #region Constructor
        public ContentAggregationToolPart()
        {
            this.Title = "Custom Configuration";

        }
        #endregion


        #region ToolPart Events
        protected override void CreateChildControls()
        {

            toolPartPanel = new Panel();
            toolPartPanel.ID = "toolPartPanel";

            toolPartPanel.Controls.Add(LoadToolPartUserControl((ContentAggregationWebPart)this.ParentToolPane.SelectedWebPart));
            Controls.Add(toolPartPanel);


            base.CreateChildControls();
        }

        public override void ApplyChanges()
        {

            ContentAggregationWebPart currWebPart = (ContentAggregationWebPart)this.ParentToolPane.SelectedWebPart;
            ApplyToolPartChanges((ContentAggregationWebPart)this.ParentToolPane.SelectedWebPart);



        }
        #endregion


        #region Private Methods

        private Control LoadToolPartUserControl(params object[] constructorParameters)
        {
            List<Type> constParamTypes = new List<Type>();
            foreach (object constParam in constructorParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }


            UserControl toolPartUserControl = (UserControl)Page.LoadControl("~/_controltemplates/ContentControls/ToolPartUserControl.ascx");
            toolPartUserControl.ID = "toolPartUserControl";

            ConstructorInfo constructor = toolPartUserControl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());
            constructor.Invoke(toolPartUserControl, constructorParameters);

            return toolPartUserControl;



        }

        private void ApplyToolPartChanges(params object[] methodParameters)
        {
            List<Type> constParamTypes = new List<Type>();
            foreach (object constParam in methodParameters)
            {
                constParamTypes.Add(constParam.GetType());
            }

            UserControl toolPartUserControl = (UserControl)this.FindControl("toolPartPanel").FindControl("toolPartUserControl");
            MethodInfo ApplyChangesMethod = toolPartUserControl.GetType().BaseType.GetMethod("ApplyToolPartChanges");
            ApplyChangesMethod.Invoke(toolPartUserControl, methodParameters);



        }
        #endregion


    }
}
